#include "queue.h"
#include <stdlib.h>

void queue_init(Queue** q) {

	*q = (Queue*)malloc(sizeof(Queue));

	(*q)->length = 0;
	(*q)->head = NULL;
	(*q)->tail = NULL;

}

void queue_cleanup(Queue* q) {

	while(queue_size(q) != 0)
		queue_pop(q);

	free(q);
	
}

int queue_size(Queue* q) {

	return q->length;
	
}

void queue_push(Queue* q, void* data) {

	Element *temp, *fresh;

	fresh = (Element*)malloc(sizeof(Element));
	fresh->data = data;
	fresh->next = NULL;
	
	if(q->length != 0) {

		temp = q->tail;

		temp->next = fresh;

		q->tail = fresh;

	}
	else {

		q->head = fresh;
		q->tail = fresh;
		
	}

	q->length++;
	
}

void* queue_pop(Queue* q) {

	Element* temp;
	void* data;

	if(q->length == 0)
		return NULL;

	temp = q->head;

	q->head = temp->next;

	data = temp->data;

	free(temp);

	q->length--;
	
	return data;

}

void* queue_peek(Queue* q) {

	if(q->length == 0)
		return NULL;
	else
		return q->head->data;

}